package com.lych.server.controller;

import com.lych.commons.utils.JwtUtils;
import com.lych.model.domain.UserInfo;
import com.lych.server.interceptor.UserHolder;
import com.lych.server.service.UserInfoService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 保存用户信息
     * @param userInfo
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo){
        //解析token
        Long id = UserHolder.getUserId();
        //向UserInfo中设置用户id
        userInfo.setId(id);
        //调用service保存
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 首次登录---补充头像
     * @param headPhoto
     * @return
     * @throws IOException
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity loginReginfoHead(MultipartFile headPhoto) throws IOException {
        //解析token
        Long id = UserHolder.getUserId();
        //调用service方法
        userInfoService.updatehead(headPhoto,id);
        return ResponseEntity.ok(null);
    }
}
